/**
 *  A collaboration diagram is a composite aggregate of any number of
 *	model elements.
 *
 *  @author	K Barclay
 */



package diagram;



import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.*;

import window.CollieDiagramPanel;
import window.CollieModelPanel;



public class CollaborationDiagram implements PropertyChangeListener{
	
	private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
	
	private SelectionListener selectionListener = new SelectionListener();
	
	private ElementSelectionSupport selectionSupport = new ElementSelectionSupport();
	
	
	
	public void addElementSelectionListener(ElementSelectionListener listener) {
		selectionSupport.addElementSelectedListener(listener);
	}
	
	public void removeElementSelectionListener(ElementSelectionListener listener) {
		selectionSupport.removeElementSelectedListener(listener);
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		changeSupport.addPropertyChangeListener(CollieModelPanel.MODEL_DIRTIED_CHANGED_PROPERTY, listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		changeSupport.removePropertyChangeListener(CollieModelPanel.MODEL_DIRTIED_CHANGED_PROPERTY, listener);
	}

    public void		add(ModelElement element) {
    	if(element != null) {
    	    modelElements.add(element);
    	    element.addElementSelectionListener(selectionListener);
    	    element.addPropertyChangeListener(this);
    	    changeSupport.firePropertyChange(CollieModelPanel.MODEL_DIRTIED_CHANGED_PROPERTY, null, this);
    	}
    }
    
    public void remove(ModelElement element) {
    	
    }
    
    public ModelElement		hitTest(int mouseX, int mouseY) {
    	final int size = modelElements.size();
    	for(int k = size - 1; k >= 0; k--) {
    	    ModelElement element = (ModelElement)modelElements.get(k);
    	    if(element.isHit(mouseX, mouseY))
    	    	return element;
    	}
    	return null;
    }
    
    public ModelElement		hitTest(Point mousePoint) {
    	return hitTest(mousePoint.x, mousePoint.y);
    }
    
    
    
    public void		draw(Graphics graphicsContext) {
    	final int size = modelElements.size();
    	for(int k = 0; k < size; k++) {
    	    ModelElement element = (ModelElement)modelElements.get(k);
    	    element.draw(graphicsContext);
    	}
    }
    
    
    
    public void			relocateElement(ModelElement element) {
    	if(element != null) {
    	    modelElements.remove(element);
    	    modelElements.add(element);
    	}
    }



    public void		deselectAll() {
    	final int size = modelElements.size();
    	for(int k = 0; k < size; k++) {
    	    ModelElement element = (ModelElement)modelElements.get(k);
    	    element.setSelected(false);
    	}
    }
    
    
    
// ---------- properties ----------------------------------

    private ArrayList		modelElements	= new ArrayList(8);

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		// Lorsqu'un des éléments du modèle change, il faut indiquer que le modèle a changé
		changeSupport.firePropertyChange(CollieModelPanel.MODEL_DIRTIED_CHANGED_PROPERTY, null, this);
	}

    
	
	private class SelectionListener implements ElementSelectionListener {

		@Override
		public void elementSelected(ElementSelectionEvent event) {
			selectionSupport.fireElementSelectedEvent(event);
		}

		@Override
		public void elementDeselected(ElementSelectionEvent event) {
			selectionSupport.fireElementDeselectedEvent(event);
		}
		
	}
}	// class: CollaborationDiagram

// ============================================================================
